package org.medicalinstrument.action;

import com.opensymphony.xwork2.ActionSupport;
import org.medicalinstrument.bean.PageBean;
import org.medicalinstrument.bean.Product;
import org.medicalinstrument.service.ProductService;
import org.medicalinstrument.util.Base64Image;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author cirly
 * @Description this is demotest class.aims to interact with the web front end
 * @Date 14:31 2018/10/28
 * @Version 1.0
 */
public class ProductAction extends ActionSupport {
    private static final long serialVersionUID = 1L;
    //private File files;
    private String returnString;
    private String productQR;//主关键字
    private String productType;
    private String productName;
    private String productColor;
    private String productFactory;
    private String productDate;
    private String productPhoto;
    private Integer saleableInformation;
    private Integer productNumber;
    private Float salePrice;
    private Float rentPrice;
    private String isBroken;
    private String brokenDate;
    private Float rentMoney;
    private Integer state;
    private List<File> upImg;
    private int currentPage; //当前页码
    private int rows;//每页显示的记录数
    private String conType;
    private String conName;
    private String introduction;

    public String getIntroduction() {
        return introduction;
    }

    public void setIntroduction(String introduction) {
        this.introduction = introduction;
    }

    public String getConType() {
        return conType;
    }

    public void setConType(String conType) {
        this.conType = conType;
    }

    public String getConName() {
        return conName;
    }

    public void setConName(String conName) {
        this.conName = conName;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    private ProductService service = new ProductService();

    public String getReturnString() {
        return returnString;
    }

    public void setReturnString(String returnString) {
        this.returnString = returnString;
    }

    public String getProductQR() {
        return productQR;
    }

    public void setProductQR(String productQR) {
        this.productQR = productQR;
    }

    public String getProductType() {
        return productType;
    }

    public void setProductType(String productType) {
        this.productType = productType;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getProductColor() {
        return productColor;
    }

    public void setProductColor(String productColor) {
        this.productColor = productColor;
    }

    public String getProductFactory() {
        return productFactory;
    }

    public void setProductFactory(String productFactory) {
        this.productFactory = productFactory;
    }

    public String getProductDate() {
        return productDate;
    }

    public void setProductDate(String productDate) {
        this.productDate = productDate;
    }

    public String getProductPhoto() {
        return productPhoto;
    }

    public void setProductPhoto(String productPhoto) {
        this.productPhoto = productPhoto;
    }

    public Integer getSaleableInformation() {
        return saleableInformation;
    }

    public void setSaleableInformation(Integer saleableInformation) {
        this.saleableInformation = saleableInformation;
    }

    public Integer getProductNumber() {
        return productNumber;
    }

    public void setProductNumber(Integer productNumber) {
        this.productNumber = productNumber;
    }

    public Float getSalePrice() {
        return salePrice;
    }

    public void setSalePrice(Float salePrice) {
        this.salePrice = salePrice;
    }

    public Float getRentPrice() {
        return rentPrice;
    }

    public void setRentPrice(Float rentPrice) {
        this.rentPrice = rentPrice;
    }

    public String getIsBroken() {
        return isBroken;
    }

    public void setIsBroken(String isBroken) {
        this.isBroken = isBroken;
    }

    public String getBrokenDate() {
        return brokenDate;
    }

    public void setBrokenDate(String brokenDate) {
        this.brokenDate = brokenDate;
    }

    public List<File> getUpImg() {
        return upImg;
    }

    public void setUpImg(List<File> upImg) {
        this.upImg = upImg;
    }

    public Float getRentMoney() {
        return rentMoney;
    }

    public void setRentMoney(Float rentMoney) {
        this.rentMoney = rentMoney;
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    /**
     * 分页显示商品信息
     */
    public String showByPage() {
        PageBean<Product> page = new PageBean<>();
//        System.out.println(conType);
//        System.out.println(conName);
        page.setCurrentPage(currentPage);
        page.setRows(rows);
        System.out.println(page);
        returnString = service.showByPage(page, conType, conName);
        return SUCCESS;
    }
    public String showByPage1() {
        PageBean<Product> page = new PageBean<>();
//        System.out.println(conType);
//        System.out.println(conName);
        page.setCurrentPage(currentPage);
        page.setRows(rows);
        System.out.println(page);
        returnString = service.showByPage1(page, conType, conName);
        return SUCCESS;
    }

    /**
     * 通过二维码回显商品信息
     */
    public String ediProduct() {
        returnString = service.getByProductQR(productQR);
        System.out.println(returnString);
        if (returnString != "") {
            return SUCCESS;
        } else
            return ERROR;

    }

    /**
     * 通过二维码删除商品
     */
    public String delProduct() {
        returnString = service.delByProductQR(productQR);
        return SUCCESS;
    }
    /*报废商品总数*/
    public String brokenTotal(){
        returnString = String.valueOf(service.brokenTotal());
        System.out.println(returnString);
        return SUCCESS;
    }

    /**
     * @Author cirly
     * 获取文件后缀名
     */
    private static String getExtention(String fileName) {
        int pos = fileName.lastIndexOf("/");
        return "." + fileName.substring(pos + 1);
    }

    /**
     * 修改商品信息
     */
    public String update() {
        Product pro = new Product();
        pro.setProductQR(productQR);
        pro.setIsBroken(isBroken);
        pro.setSaleableInformation(saleableInformation);
        pro.setRentMoney(rentMoney);
        pro.setRentPrice(rentPrice);
        pro.setSalePrice(salePrice);
        pro.setIntroduction(introduction);
        System.out.println("qqqq"+introduction);
        System.out.println("qqqq"+isBroken);
        returnString = service.updateProduct(pro);
        if (returnString.equals("insert_success")) {
            return SUCCESS;
        } else {
            return ERROR;
        }

    }

    /**
     * 上传商品
     */
    Base64Image base = new Base64Image();

    public String upload() throws IOException, SQLException {
        //System.out.println(upImg.size());
        Product pro = new Product();
        // System.out.println(upImgFileName[0]);
        pro.setProductQR(productQR);
        pro.setIsBroken(isBroken);
        pro.setProductFactory(productFactory);
        pro.setProductColor(productColor);
        pro.setProductName(productName);
        pro.setProductType(productType);
        pro.setRentMoney(rentMoney);
        pro.setRentPrice(rentPrice);
        pro.setProductDate(productDate);
        pro.setSalePrice(salePrice);
        pro.setSaleableInformation(saleableInformation);
        pro.setProductNumber(productNumber);
        pro.setProductPhoto(base.GetImageStr(upImg.get(0)));
        pro.setState(state);
        pro.setIntroduction(introduction);
        System.out.println("action...");
        System.out.println(pro.toString());
        returnString = service.addProduct(pro);
        System.out.println("action"+returnString);
        if (returnString.equals("insert_success")) {
            return SUCCESS;
        } else {
            return ERROR;
        }

    }


    /**
     * @Author cirly
     * @Description 所有商品，通过ajax传到前端
     * @Date 21:23 2018/11/4
     * @Modify by
     * @Version 1.0
     */
    public String productList() {
        returnString = service.getAll();
        //System.out.println("adction---"+returnString);
        //  System.out.println(returnString);
        if (returnString.equals("noproduct")) {
            return ERROR;
        } else {
            return SUCCESS;
        }
    }

    /**
     * @Author: kongHuan
     * @Date: 2019/3/10 17:13
     * @Description:通过二维码获取商品信息
     */

    public String getById(){
        returnString = service.getByProductQR(productQR);
        if(returnString != ""){
            return SUCCESS;
        }else
            return ERROR;

    }
}
